package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.CompanyInfo;
import com.suyuan.entity.ProducetypeInfo;
import com.suyuan.entity.ProductInfo;
import com.suyuan.service.ButcherProductService;
import com.suyuan.service.CompanyService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 屠宰包装成商品
 */
@Controller
@RequestMapping("/admin/Butcher")
public class ButcherProductController {

    @Resource
    private ButcherProductService butcherProductService;
    @Resource
    private Map<String,Object> msg;
    /**
     * 多条件查询生猪商品信息
     * @param productID
     * @param pigID
     * @param compID
     * @param typeID
     * @param page
     * @param modelMap
     * @return
     */
    @RequestMapping("findByCondition")
    public String findByCondition(Integer productID, Integer pigID, Integer compID, Integer typeID, Page page, ModelMap modelMap){
        PageInfo pageInfo = butcherProductService.findByCondition(productID,pigID,compID,typeID,page);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("productID",productID);
        modelMap.addAttribute("compID",compID);
        modelMap.addAttribute("typeID",typeID);
        modelMap.addAttribute("pigID",pigID);
        modelMap.addAttribute("page",page);
        return "butcherProduct_list";
    }


    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping("toAddPage")
    public String toAddPage( ModelMap modelMap){
       //查找所有屠宰公司
       List<CompanyInfo> companyInfoList = butcherProductService.findAllCompany();
       //查找所有产品类型
       List<ProducetypeInfo> producetypeInfoList  = butcherProductService.findAllProduce();
       modelMap.addAttribute("companyInfoList",companyInfoList);
       modelMap.addAttribute("producetypeInfoList",producetypeInfoList);
        return "butcherProduct_add";
    }


    /**
     * 添加产品信息
     * @param productInfo
     * @return
     */
    @RequestMapping("addInfo")
    @ResponseBody
    public Object addInfo(ProductInfo productInfo){
        butcherProductService.addProductInfo(productInfo);
        return 1;
    }


    /**
     * 跳转编辑页面
     * @param gid
     * @return
     */
    @RequestMapping("toedit")
    public String toeditPage(Integer gid,ModelMap modelMap){
        ProductInfo productInfo = butcherProductService.findByProductID(gid);
        //查找所有屠宰公司
        List<CompanyInfo> companyInfoList = butcherProductService.findAllCompany();
        List<ProducetypeInfo> producetypeInfoList  = butcherProductService.findAllProduce();
        modelMap.addAttribute("productInfo",productInfo);
        modelMap.addAttribute("companyInfoList",companyInfoList);
        modelMap.addAttribute("producetypeInfoList",producetypeInfoList);
        return "butcherProduct_edit";
    }


    /**
     * 编辑商品信息
     * @param productInfo
     * @return
     */
    @RequestMapping("edit")
    @ResponseBody
    public Object editInfo(ProductInfo productInfo){
        butcherProductService.updateInfo(productInfo);
        return 1;
    }


    /**
     * 单项删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Integer deleteInfo(Integer id){
       return  butcherProductService.deleteInfo(id);
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("deletes")
    @ResponseBody
    public Object deleteInfos(String ids){
        Integer code = butcherProductService.deleteInfos(ids);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }else{
            return JsonMessage.error("删除失败");
        }
    }


    @RequestMapping("checkPurchase")
    @ResponseBody
    public Map<String,Object> checkPur(Integer pid){
        msg = new HashMap<String, Object>();
        if(butcherProductService.checkPurcharse(pid) == false){
            msg.put("msg",1);
            return msg;
        }else{
            msg.put("message","生猪信息有效");
            return msg;
        }
    }













}
